<!-- 表单属性 -->

<template>
  <FormSlot label="表单属性" bind="attributes" noMargin required>
    <div class="TaskTemplate-TemplateProcess">
      <div class="flexCC alignCenter">
        <div class="flex10"><b>字段标识</b></div>
        <div class="flex16"><b>字段名称</b></div>
        <div class="flex6"><b>字段类型</b></div>
        <div class="flex3 alignCenter" />
      </div>

      <draggable v-model="one.attributes" :animation="200" class="TaskTemplate-TemplateProcess-list">
        <FormSlot
          v-for="(item, i) in one.attributes"
          :key="item.id"
          :bind="`child_${item.id}`"
          tip="请填写完整"
          noLabel
          required
        >
          <div class="flexCC">
            <FormInput class="flex10" :one="item" bind="attributeCode" @change="toChange(item)" />
            <FormInput class="flex16" :one="item" bind="attributeName" @change="toChange(item)" />
            <FormSelect class="flex6" :one="item" bind="attributeType" :dict="DICT.FORM_TYPE" noClear />
            <div class="flex3 alignCenter">
              <BtnText v-if="!isShow" icon="el-icon-plus" class="marginRight" @click="addChild(i)" />
              <BtnText
                v-if="!isShow"
                :disabled="one.attributes.length === 1"
                icon="el-icon-minus"
                red
                @click="delChild(i)"
              />
            </div>
          </div>
        </FormSlot>
      </draggable>
    </div>
  </FormSlot>
</template>

<script>
// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'

// ============================== 导出组件 ============================== //

export default {
  /**
   * 名称定义 (实际使用名称)
   */
  name: 'MyForm',

  /**
   * 混入列表 (配置选项合并)
   */
  mixins: [CORE.mixWeb()],

  /**
   * 本地方法 (轻量重要在前)
   */
  methods: {
    /**
     * 子项改变
     */
    toChange(item) {
      const { one, $set } = this
      const readyStr = item.attributeCode && item.attributeName ? 'ready' : ''
      $set(one, `child_${item.id}`, readyStr)
    },

    /**
     * 添加子项
     */
    addChild(i) {
      const { INIT_ONE, one, $set } = this
      const child = CORE.clone(INIT_ONE.attributes[0])
      const id = `${Date.now()}`
      $set(one, `child_${id}`, '')
      one.attributes.splice(i + 1, 0, { ...child, id })
    },

    /**
     * 删除子项
     */
    delChild(i) {
      const { one } = this
      one.attributes.splice(i, 1)
    },
  },
}
</script>

<style lang="scss">
@import '@admin/views/Maintain/Task/TaskTemplate/MyModal/TemplateProcess/index.scss';
</style>
